//
//  TabBar.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/8.
//

import UIKit

class TabBar: UITabBar {
    var centerBtn : QMUIButton!
    var buttonClickBlock : (()->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 中间凸起按钮 可自定义样式
        centerBtn = QMUIButton()
        centerBtn.backgroundColor = .blue
        centerBtn.adjustsButtonWhenHighlighted = false
        centerBtn.clipsToBounds = true
        centerBtn.setImage(UIImage(named: ""), for: .normal)
        centerBtn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        addSubview(centerBtn)
        
        centerBtn.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(-30)
            // 大小可以写死，也可不写自动使用图片大小
            make.size.equalTo(CGSize(width: 60, height: 80))
        }
    }
    
    @objc func buttonClick() {
        if buttonClickBlock != nil {
            buttonClickBlock!()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if isHidden {
            return super.hitTest(point, with: event)
        }
        let view : UIView? = super.hitTest(point, with: event)
        if view == nil {
            let tempPoint : CGPoint! = centerBtn?.convert(point, from: self)
            if centerBtn.bounds.contains(tempPoint) {
                return centerBtn
            }
        }
        return view
    }
}
